import React, { Component } from 'react'

export default class App extends Component {
  // 受控：表单的 value 受到了状态的控制（状态不变，value 也变不了）
  state = {
    username: 'dva',
    content: '🧐',
    frame: 'react',
    sex: 'girl',
  }
  handleInput = (e) => {
    // e.target.value 赋值给状态 username 就 ok 啦
    // console.log(e.target.value)
    // 问题：违背了状态的不可变性；视图也不会更新
    // this.state.username = e.target.value
    this.setState({ username: e.target.value })
  }
  handleTextArea = (e) => {
    this.setState({ content: e.target.value })
  }
  handleSelect = (e) => {
    this.setState({ frame: e.target.value })
  }
  handleRadio = (e) => {
    this.setState({ sex: e.target.value })
  }
  render() {
    return (
      <ul>
        <li>
          <label htmlFor='username'>用户名</label>
          <input
            id='username'
            value={this.state.username}
            onChange={this.handleInput}
          />
        </li>
        <li>
          <label htmlFor='content'>其他信息</label>
          <textarea
            id='content'
            value={this.state.content}
            onChange={this.handleTextArea}
          ></textarea>
        </li>
        <li>
          <label htmlFor='frame'>框架</label>
          <select
            id='frame'
            value={this.state.frame}
            onChange={this.handleSelect}
          >
            <option value='vue'>vue</option>
            <option value='react'>react</option>
            <option value='angular'>angular</option>
          </select>
        </li>
        <li>
          <input
            type='radio'
            id='boy'
            value='boy'
            checked={this.state.sex === 'boy'}
            onChange={this.handleRadio}
          />
          <label htmlFor='boy'>男</label>
          <input
            type='radio'
            id='girl'
            value='girl'
            checked={this.state.sex === 'girl'}
            onChange={this.handleRadio}
          />
          <label htmlFor='girl'>女</label>
          <input
            type='radio'
            id='unknown'
            value='unknown'
            checked={this.state.sex === 'unknown'}
            onChange={this.handleRadio}
          />
          <label htmlFor='unknown'>未知</label>
        </li>
      </ul>
    )
  }
}
